/** CS2340 Spring 2011 Project
 *  Team: 40 Jiggawatt Hard Driver
 *  Suk Hwan Hong
 *  Rhys Saraceni
 *  Travis Cooper
 *  Luke Stubb
 *
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0 
 */

package edu.gatech.jiggawatt.office.model;

import java.io.Serializable;

/**
 * Represents Daily Schedule
 * @author 40 Jiggawatt Hard Driver
 * @version 1.0
 *
 */
public class DailySchedule implements Serializable // $codepro.audit.disable declareDefaultConstructors
{
	private int year;
	private int month;
	private int day;
	private int dayOfWeek; //1 = Sunday, 2 = Monday, 3 = Tuesday, ...
	
	//indexed by time slots
	//[0] = 9:00, [1] = 9:30, [2] = 10:00, [3] = 10:30
	//[4] = 11:00, [5] = 11:30, LUNCHTIME, [6] = 13:00, [7] = 13:30
	//[8] = 14:00, [9] = 14:30, [10] = 15:00, [11] = 15:30, [12] = 16:00
	//[13] = 16:30, [14] = 17:00, [15] = 17:30
	private Appointment[] appointments;
	
	/**
	 * Creates daily schedule
	 * @param year
	 * @param month
	 * @param day
	 * @param dayOfWeek (1 = Sunday, 2 = Monday, 3 = Tuesday...)
	 */
	public DailySchedule(int year, int month, int day, int dayOfWeek)
	{
		this.year = year;
		this.month = month;
		this.day = day;
		this.dayOfWeek = dayOfWeek;
		appointments = new Appointment[16]; // $codepro.audit.disable numericLiterals
	}
	
	/**
	 * add appointment
	 * @param app appointment to add
	 * @return true if successful, false otherwise
	 */
	public boolean addAppointment(Appointment app) // $codepro.audit.disable booleanMethodNamingConvention
	{
		int hours = app.getTime().getHours();
		int minutes = app.getTime().getMinutes();
		int slotIndex = (hours * 60 + minutes - 540) / 30; // $codepro.audit.disable numericLiterals
		
		if (null != appointments[slotIndex])
		{
			return false;
		}
		else
		{
			appointments[slotIndex] = app;
			return true;
		}		
	}
	
	/**
	 * removes appointment
	 * @param date
	 * @return appointment that was removed
	 */
	public Appointment removeAppointment(Time date)
	{
		int hours = date.getHours();
		int minutes = date.getMinutes();
		int slotIndex = (hours * 60 + minutes - 540) / 30; // $codepro.audit.disable numericLiterals
		
		Appointment oldAppointment;
		
		if (null == appointments[slotIndex])
		{
			return null;
		}
		else
		{
			oldAppointment = appointments[slotIndex];
			appointments[slotIndex] = null;
			return oldAppointment;
		}		
	}
	
	/**
	 * changes appointment
	 * @param oldApp old appointment to change
	 * @param newApp new appointment that contains new information
	 * @return true if success, false otherwise
	 */
	public boolean changeAppointment(Appointment oldApp, Appointment newApp) // $codepro.audit.disable booleanMethodNamingConvention
	{
		if (null == removeAppointment(oldApp.getTime()))
		{
			return false;
		}
		else
		{
			return addAppointment(newApp);
		}
	}
	
	/**
	 * gets appointment
	 * @param date
	 * @return appointment at that date (null if none exists)
	 */
	public Appointment getAppointment(Time date)
	{
		int hours = date.getHours();
		int minutes = date.getMinutes();
		int slotIndex = (hours * 60 + minutes - 540) / 30; // $codepro.audit.disable numericLiterals
		
		return appointments[slotIndex];
	}
	
	/**
	 * gets array of daily appointments
	 * @return array of daily appointments
	 */
	public Appointment[] getAppointments()
	{
		return appointments;
	}
	
	/**
	 * gets day
	 * @return day
	 */
	public int getDay()
	{
		return day;
	}
	
	/**
	 * gets month
	 * @return month
	 */
	public int getMonth()
	{
		return month;
	}
	
	/**
	 * gets year
	 * @return year
	 */
	public int getYear()
	{
		return year;
	}
	
	/**
	 * returns string representation of day
	 * @return string representation of day
	 */
	public String toString()
	{
		return day + "";
	}
}
